require.config({baseUrl: '/js/lib',urlArgs: "v0.0.1"});
require(['zepto', 'lib'], function($, lib) {
  var fn = {}, lib = new lib();
  $.extend(fn, {
    totalBalance: 0,
    init: function(){
      
      
      this.getAlipayAccount();
      this.getAccountInfo();
      this.bindEvent();
    },
    bindEvent: function(){
      var _t = this;
      $(document).on('blur', 'input[name=withDrawals]', function(){
        var num = Number($("input[name=withDrawals]").val().trim());
        if(isNaN(num) || num <= 0){
          $("input[name=withDrawals]").val('0');
          return;
        }
        $("input[name=withDrawals]").val(Number($("input[name=withDrawals]").val().trim()).toFixed(2));
      });
      $(document).on('tap', '.ui-red-botton:not(.ui-ing)', function(data){
        var num = Number($("input[name=withDrawals]").val().trim()), _this = this;
        if(isNaN(num)){
          lib.showMessage("请正确输入提现金额");
          return;
        }
        if(num <= 0){
          lib.showMessage("提现金额不能小于0.01");
          return;
        }else if(num > _t.totalBalance){
          lib.showMessage("提现金额不能大于账户余额");
          return;
        }
        $(_this).addClass("ui-ing");
        lib.onLoading();
        lib.ajx(lib.getReq().ser + 'sellerCenter/drawCash.action', {amount: num}, function(data){
          lib.showMessage(data.info)
          if(data.infocode === '0'){
            _t.getAccountInfo();
            $("input[name=withDrawals]").val('');
          }
          $(_this).removeClass("ui-ing");
        }, function(){
          lib.showMessage("网络延时，请稍后再试");
          $(_this).removeClass("ui-ing");
        });
      });
      
      $(document).on('tap', ".ui-all-money", function(){
        $("input[name=withDrawals]").val(_t.totalBalance);
      });
    },
    getAccountInfo: function(){
      var _t = this;
      lib.onLoading();
      lib.ajx(lib.getReq().ser + 'sellerCenter/getAccountInfo.action', {searchType: 2}, function(data){
        if(data.infocode === '0'){
          _t.totalBalance = Number(data.info);
          $(".ui-balance").text(data.info);
        }
        else lib.showMessage(data.info);
      }, function(){
        lib.showMessage("网络延时，请稍后再试");
      });
    },
    getAlipayAccount: function(){
      lib.ajx(lib.getReq().ser + 'sellerCenter/getAlipayAccount.action', {}, function(data){
        if(data.infocode === '0'){
          $(".ui-zhifubao li").html(`<span>${data.info}</span>`);
        }
        else if(data.infocode == '2') location.href = './login.html?backUrl=' + lib.getBackUrl();
        else if(data.infocode == '3') location.href = './account_add.html';
        else lib.showMessage(data.info);
      }, function(){
        lib.showMessage("网络延时，请稍后再试");
      });
    }
  });
  
  fn.init();
});